Skip to content

fix(#298): resolve entry.sh path without GNU readlink - #324

Open
macoladik wants to merge 2 commits into
zerocracy:masterfrom
macoladik:298
Open

macoladik wants to merge 2 commits into
zerocracy:masterfrom
macoladik:298

Conversation

@macoladik

Copy link
Copy Markdown
Contributor

entry.sh resolved its own directory with readlink -f, which BSD readlink on macOS rejects, so the documented entry point died with "illegal option -- f" before a single judge ran, while the README promised only Ruby and Bundler. It now walks symlink chains with plain readlink and cd … && pwd -P, and a slashless $0 found through PATH resolves to the script instead of the working directory.

test_resolves_relative_symlink_chain_without_gnu_readlink runs the script end to end with a readlink shim that rejects -f, so Linux CI covers the BSD host case; test_resolves_slashless_script_name_found_through_path pins the PATH invocation.

Closes #298

@macoladik
macoladik requested a review from yegor256 as a code owner November 7, 2026 22:04

@macoladik macoladik left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nine findings: how the new resolution behaves when $0 is not the script, one silent failure path, and six test gaps that let the readlink/cwd regressions back in.

Comment thread entry.sh
self=$0
# bash passes a slashless $0 when the script is found via PATH; resolve it
# unless a same-named file in cwd is what bash actually opened.
if [ ! -f "${self}" ] && resolved=$(command -v -- "${self}"); then

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When $0 is not the script - bash -s < entry.sh job-42 /var/base, or a wrapper doing bash -c 'source ./entry.sh' job-42 ... - $0 is bash, [ -f bash ] is false and command -v bash succeeds, so self becomes /bin (or /usr/bin on Linux) and judges is invoked as --lib /bin/lib /bin/judges. The command -v judges check at line 25 already passed, so the job dies inside judges with a message that never mentions $0. Minimal fix, after the loop: [ -d "${self}/judges" ] || { echo >&2 "Cannot locate the swarm directory for '$0' (resolved to '${self}')"; exit 1; }.

Comment thread entry.sh
*) self=$(dirname "${self}")/${link} ;;
esac
done
self=$(cd "$(dirname "${self}")" && pwd -P)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set -e turns a failed cd into a bare exit 1 with no output: run with $0=/mnt/job/entry.sh once the job volume is unmounted, or when the directory loses +x, and the swarm run fails with an empty log - judges never starts and nothing says why. Fix: self=$(cd "$(dirname "${self}")" && pwd -P) || { echo >&2 "Cannot enter '$(dirname "${self}")'"; exit 1; }.

Comment thread test/test_entry.rb

def test_keeps_own_directory_when_slashless_script_is_not_in_path
Dir.mktmpdir do |dir|
FileUtils.cp(File.expand_path('../entry.sh', __dir__), File.join(dir, 'entry.sh'))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test cannot fail for the behaviour it names: bin, the only PATH entry prepended in run_entry (line 97), contains just judges, so command -v entry.sh never succeeds here and the [ ! -f "${self}" ] guard at entry.sh:33 is irrelevant - deleting that guard keeps this test green. To lock the rule, put a second entry.sh with its own lib/ inside bin and assert the cwd copy's directory wins (bash opens the literal name first, so the PATH copy is never executed).

Comment thread test/test_entry.rb
home = File.join(dir, 'home')
cwd = File.join(dir, 'cwd')
FileUtils.mkdir_p([home, cwd])
passed = run_entry(bin, home, File.join(dir, 'args.txt'), script: %w[bash entry.sh], chdir: cwd)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The branch the guard exists for is untested: a copy of entry.sh in the cwd and on PATH. An implementation that always prefers the PATH lookup (resolved=$(command -v -- "${self}") || true; self=${resolved:-$0}) passes this test (entry.sh only on PATH) and the test at line 58 (entry.sh only in cwd), yet resolves the wrong directory whenever both exist. Add that case here, with distinct lib/ directories for each copy.

Comment thread test/test_entry.rb
stub = File.join(bin, 'readlink')
File.write(stub, <<~SH)
#!/bin/sh
[ "$1" = '-f' ] && { echo 'readlink: illegal option -- f' >&2; exit 1; }

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The stub rejects only -f, so every other GNU-only flag slips through: replacing the readlink call at entry.sh:37 with readlink -e or readlink --canonicalize keeps this test green and re-breaks macOS (issue #298). Reject any option: [ "${1#-}" != "$1" ] && { echo 'readlink: illegal option' >&2; exit 1; }.

Comment thread test/test_entry.rb
File.write(stub, <<~SH)
#!/bin/sh
[ "$1" = '-f' ] && { echo 'readlink: illegal option -- f' >&2; exit 1; }
exec /usr/bin/readlink "$@"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hardcoding /usr/bin/readlink breaks on hosts that ship it as /bin/readlink only (no usr-merge): the stub then exits 127, link in entry.sh is empty, resolution silently stops at the symlink's own directory and the test fails with a wrong path instead of a portability signal. Resolve the real binary once in Ruby (File.realpath('/bin/readlink'), falling back to /usr/bin/readlink) and exec that path in the stub.

Comment thread test/test_entry.rb

def run_entry(bin, home, args, script: './entry.sh')
def run_entry(bin, home, args, script: './entry.sh', chdir: Dir.pwd)
stdout, stderr, status = Open3.capture3(

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The judges stub truncates its output file (File.write at line 16), so a second invocation is invisible: a retry loop, or a pre-flight judges --version added to entry.sh, leaves every test green while only the last call is asserted. Make the stub append (File.open(ENV.fetch('JUDGES_ARGS_FILE'), 'a')) and assert the expected number of invocation blocks.

Comment thread test/test_entry.rb
@@ -31,6 +30,43 @@ def assert_real_path_used(passed)
assert_equal(File.expand_path('../judges', __dir__), passed[passed.index('--lib') + 2])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

passed.index('--lib') is nil when the flag is dropped or renamed, so the regression raises NoMethodError: undefined method '+' for nil instead of naming the problem - both assertions here do it. Guard first: i = passed.index('--lib'); refute_nil(i, "--lib not passed: #{passed.inspect}") and index from i.

Comment thread test/test_entry.rb
judges_stub(dir), "#{dir}/home", "#{dir}/args.txt", script: %w[bash entry.sh], chdir: dir
)

assert_equal(File.join(File.realpath(dir), 'lib'), passed[passed.index('--lib') + 1])

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test creates judges/ (line 61) but asserts only the --lib value, so the second path the script passes - ${self}/judges from entry.sh:46 - is unverified: a regression passing ./judges or the cwd instead of ${self}/judges stays invisible. Add assert_equal(File.join(File.realpath(dir), 'judges'), passed[passed.index('--lib') + 2]).

@yegor256 yegor256 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The directory lookup still depends on the caller's CDPATH, which can send judges an invalid or unrelated swarm path.

Comment thread entry.sh
*) self=$(dirname "${self}")/${link} ;;
esac
done
self=$(cd "$(dirname "${self}")" && pwd -P)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Clear CDPATH before resolving the script directory. With an exported CDPATH=. and a relative invocation such as bash swarm/entry.sh job-42 /existing/home, cd swarm prints its destination before pwd -P, so self contains two lines and both paths passed to judges are invalid; a CDPATH pointing elsewhere can also select a different swarm directory. Unset CDPATH inside this command substitution before calling cd, and add a regression test invoking the script through a relative subdirectory with CDPATH set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

entry.sh requires GNU readlink and fails on systems with BSD readlink

2 participants